<?php
/**
 *
 * Complexity WordPress Theme
 * Cycle Slideshow
 *
 * This function utilizes the Slide custom post
 * type to create a slideshow with the jQuery
 * Cycle plugin.
 *
 * @author  Jason Bobich
 *
 */

function complexity_slideshow($options) {

    //Innitiate $output
    $output = '';

    $slideshow = $options['slideshow'];
    $transition = $options['transition'];
    $speed = $options['speed'];
    $size = $options['size'];

    $id = mt_rand();

    if($speed != '0'){
       $speed = $speed.'000';
    }

    if($slideshow != 'all'){
        $slideshowString = "&slideshows=".$slideshow;
    } else {
    	$slideshowString = "";
    }

    $slideshow_query = new WP_Query('showposts=-1&post_type=slide&order=ASC&orderby=menu_order&nopaging=true'.$slideshowString);

    if($slideshow_query->have_posts()) {
        
        $output .= '<script type="text/javascript">'."\n";
            $output .= 'jQuery.noConflict()(function($){';
                $output .= '$(document).ready(function() {'."\n";
                    $output .= '$("#slideshow").cycle({'."\n";
                        $output .= 'fx:     "'.$transition.'",'."\n";
                        $output .= 'timeout: '.$speed.",\n";
                        $output .= 'pager:  "#slideshow-nav-inner",'."\n";
                        $output .= 'pagerAnchorBuilder:  paginate,'."\n";
                        $output .= 'speed:  "2000",';
                        $output .= 'pause: 1,';
                        $output .= 'easing: "easeInOutQuint"'."\n";
                    $output .= '});'."\n";
                $output .= '});'."\n";
            $output .= '});'."\n";
        $output .= '</script>'."\n";
        
        $output .= '<div id="slideshow">';

        while ($slideshow_query->have_posts()) {

            $slideshow_query->the_post();

            $output .= '<div class="slide">';

            global $post;
            $slide_type = get_post_meta($post->ID, 'themeblvd_slide_type', true);

            if($slide_type == 'true') {

                if( get_post_meta($post->ID, 'themeblvd_slide_link', true) ){
                    $output .= '<a href="'.get_post_meta($post->ID, 'themeblvd_slide_link', true).'" title="'.get_the_title().'" style="display:block;">';
                }

                if ( has_post_thumbnail() ) {
                   $output .= get_the_post_thumbnail($post->ID, $size);
                }

                if( get_post_meta($post->ID, 'themeblvd_slide_description', true) ){
                    $output .= '<div class="description">';
                        $output .= '<div class="pad">';
                            $output .= get_post_meta($post->ID, 'themeblvd_slide_description', true);
                        $output .= '</div><!-- .pad (end) -->';
                    $output .= '</div><!-- .description (end) -->';
                }

                if(get_post_meta($post->ID, 'themeblvd_slide_link', true)){
                    $output .= '</a>';
                }


            } else {

                $output .= apply_filters( 'the_content', get_the_content() );

            } // end slide_type

            $output .= '</div><!-- .slide (end) -->';

       } //End loop

        $output .= '</div><!-- #slideshow (end) -->';

        $output .= '<div id="slideshow-nav">';
            $output .= '<ul id="slideshow-nav-inner">';
                $output .= '<li style="display:none;"><!-- Validaiton Only --></li>';
            $output .= '</ul>';
        $output .= '</div><!-- .slideshow-nav(end) -->';

    } else {
        $output .= '<p class="warning">';
            $output .= __("There are no slides to display.", "themeblvd");
        $output .= '</p>';
    }
	
	//Reset post data
	wp_reset_postdata();
	
    return $output;

##################################################################
} # end complexity_slideshow function
##################################################################
?>